*use "C:\SPSS\V\VIOLENCE INTERACTION.DTA"
import spss "C:\SPSS\V\PLEGITX INTERACTION.SAV", clear
rename *, upper
* ANALYSIS OF TABLE 2 OF PLEGITX PAPER
* FOR TABLE C.1 IN APPENDIX C 
summarize [aweight=WEIGHTT4]
graph bar (mean) PLEGITX, over (PEXPERIENCE) 
*graph bar (mean) PLEGITX, over (PEXPERIENCE) [aweight=WEIGHTT4] 
CONPUTE

ssc install coefplot
reg  PLEGITX PEXPERIENCE LFATE  ///
  VEXPER2 ///
  IDX ///
  BLMKNOW BLM CONBLM RACISM PARTYID  POLVIEWS ///
  GENDER AGE7 EDUC5  INCOME9   SOUTH ///
  METRO INTERNET  HOMEOWND ATTEND BAGAIN RELIGX ///
  OPLEADD   [aweight=WEIGHTT4]

reg  PLEGITX C.PEXPERIENCE##C.LFATE  ///
  VEXPER2 ///
  IDX ///
  BLMKNOW BLM CONBLM RACISM PARTYID  POLVIEWS ///
  GENDER AGE7 EDUC5  INCOME9   SOUTH ///
  METRO INTERNET  HOMEOWND ATTEND BAGAIN RELIGX ///
  OPLEADD   [aweight=WEIGHTT4]
summarize VEXPER2
summarize IDX
margins, dydx(PEXPERIENCE) at(LFATE=(0 (.2) 1)) 
marginsplot
interflex     PLEGITX PEXPERIENCE ///
  LFATE ///
  VEXPER2 IDX  ///
  BLMKNOW BLM CONBLM RACISM PARTYID  POLVIEWS ///
  GENDER AGE7 EDUC5  INCOME9   SOUTH ///
  METRO INTERNET  HOMEOWND ATTEND BAGAIN RELIGX ///
  OPLEADD  [aweight=WEIGHTT4], type(linear)
CONPUTE
*margins, dydx(VEXPER2) at(IDX=(0 (.05) 1) atmeans)

margins, predict(fixedonly) dydx(*) atmeans
marginsplot
conpute


coefplot, drop (_cons) xline (0)
quietly regress GVIOLENCE ///
  PEXPERIENCE VEXPER2 ///
  IDX LFATE ///
  DOGX  RACISM   PARTYID  POLVIEWS ///
  GENDER AGE7 EDUC5  INCOME9   SOUTH ///
  METRO INTERNET  HOMEOWND ATTEND BAGAIN RELIGX ///
  OPLEADD  NDURATION ///
  IDVEXPER LFVEXPER   [aweight=WEIGHTT4],robust

estat sum GVIOLENCE // SD of depvar

estimates store m1
CONPUTE

** President **
quietly regress PRESSUPX KNOWBARRETT APPROPBARRETT KNOWOVERTURN APPROPOVERTURN
 KNOWRIGGED WASRIGGED DEMTHREAT REPTHREAT RLAWX ORDERX TOLX DOGX PARTYID POLVIEWS KNOWX GENDER AGE RACED EDUC5 INCOME9 HOMEOWND METRO SOUTH INTERNET
 BAGAIN ATTEND CITIZEN [aweight = WEIGHTJAN5], robust

estat sum GVIOLENCE // SD of depvar

estimates store m1

CONPUTE

reg APPROPBARRETT SCSUPX KNOWBARRETT KNOWOVERTURN APPROPOVERTURN ///
  KNOWRIGGED WASRIGGED REPTHREAT DEMTHREAT SCJOB SCLIBD5 IDEODIF2 ///
  RLAWX ORDERX TOLX DOGX PARTYID POLVIEWS KNOWX ////
  GENDER AGE RACED EDUC5 INCOME9 HOMEOWND METRO SOUTH INTERNET /// 
  BAGAIN ATTEND NCITIZEN , robust 
coefplot, drop (_cons) xline (0)
CONPUTE




**A) DATA PRE-PROCESSING**
*a1) load the data
use https://stats.idre.ucla.edu/wp-content/uploads/2020/06/exercise, clear

*a2) label define 
label define progl 1 "jog" 2 "swim" 3 "read"
label define genderl 1 "male" 2 "female"
label values prog progl
label values gender genderl

*a3) get descriptives
summarize loss hours effort
tab gender 
tab prog

**B) PREDICTED VALUE VS SIMPLE SLOPE**

*b1) linear model of loss on hours
regress loss hours

*b2) predicted weight loss at Hours = 2
margins, at(hours=2)

*b3) overall slope of Hours
margins, dydx(hours)

*b4) exercise
margins, at(hours=(10 20))
display (54.5-29.8)/(20-10)

*b5) plotting a regression slope
margins, at(hours=(0(1)4))
marginsplot

*recase(line) takes out the dots, recase(ci) fills in the CIs
marginsplot, recast(line) recastci(rarea) ciopt(color(%30))

**C) CONTINUOUS-BY-CONTINUOUS INTERACTION**

*c1) linear model of loss on hours by effort
*regress loss c.hours c.effort c.hours#c.effort
regress loss c.hours##c.effort

*c2) be wary of extrapolation

summarize effort

*reasonable: predicted value at hours=2, effort = 30
margins, at(hours=2 effort=30)

*extrapolation: predicted value at hours=2, effort = 0
margins, at(hours=2 effort=0)

*c3) simple slopes for continuous by continuous model
summarize effort
return list
global effa = round(r(mean) + r(sd),0.1)
global eff = round(r(mean),0.1)
global effb = round(r(mean) - r(sd),0.1)
display $effa 
display $effb
 
*simple slopes at three effort levels
margins, dydx(hours) at(effort=($effa $eff $effb))

*plot continuous by continuous interaction
margins, at(hours=(0(1)4) effort=($effa $eff $effb))
marginsplot

*c4) test differences in slopes
margins, dydx(hours) at(effort=($effa $eff $effb)) pwcompare(effects)

*c5) test the difference in predicted value hour = 4
margins, at(hours=4 effort=($effa $effb)) 
margins, at(hours=4 effort=($effa $effb)) pwcompare(effects)

*c6) plot continuous by continuous interaction (publication quality)
*CHANGE
margins, at(hours=(0(1)4) effort=($effa $eff $effb))
marginsplot, recast(line) recastci(rarea)

*c7) exercise
regress loss c.hours##c.effort
margins, at(hours=(0 1) effort=(0 1)) post coeflegend
global y00 = _b[1bn._at]
global y01 =  _b[2._at]
global y10 = _b[3._at]
global y11 =  _b[4._at]
display $y00, $y01, $y10, $y11

*c8) exercise: create a plot of hours on the x-axis with effort = 0
margins, at(hours=(0(1)4) effort=0)
marginsplot 

**D) CATEGORICAL-BY-CONTINUOUS INTERACTION**
*d0) tabulate gender
tab gender
*d1)show that male =1 and female =2, so male is the reference group (R takes lowest value)
regress loss i.gender
*d21) change of base gender
regress loss ib2.gender
*d3) predict linear model
regress loss c.hours##ib2.gender
*d4) simple slopes of hours by gender
margins gender, dydx(hours)
margins, dydx(hours) over(gender)
*d5) comparing hours slopes 
margins gender, dydx(hours) pwcompare(effects)
*d6)
regress loss c.hours##ib1.gender
margins gender, dydx(hours) pwcompare(effects)
*d7) flipping the MV and the IV 
regress loss c.hours##ib2.gender
margins, dydx(gender) at(hours=(0 2 4))
*d8) plot using marginsplot (gender as moderator) 
margins gender, at(hours=(0 1 2 3 4))
*margins, at(hours=(0 1 2 3 4)) over(gender)
marginsplot 

**E) CATEGORICAL-BY-CATEGORICAL INTERACTION**
*e1) reference group choice
tab prog
tab prog, nolabel 

*e2) fit linear model
regress loss ib2.gender##ib3.prog
regress loss i.gender i.prog ib2.gender#ib3.prog
*e3) emmeans
margins gender#prog
*pairwise contrasts by prog
margins prog, dydx(gender)
*e4) exercise 
regress loss ib2.gender##ib2.prog
*e5) optional exercise  
margins gender, dydx(prog)
*e6) interaction plot
margins gender#prog
marginsplot
*e7) exercise
margins prog#gender
marginsplot



reg  GVIOLENCE  ///
  PEXPERIENCE VEXPER2 ///
  IDX LFATE ///
  DOGX  RACISM   PARTYID  POLVIEWS ///
  GENDER AGE7 EDUC5  INCOME9   SOUTH ///
  METRO INTERNET  HOMEOWND ATTEND BAGAIN RELIGX ///
  OPLEADD  NDURATION ///
  IDVEXPER LFVEXPER  [aweight=WEIGHTT4]
